 import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class StringTokenizing{

public static void main(String[] args) throws IOException {
         
         
          // The first file Steam . It is reading the PointIndex
          FileInputStream fstream1 = new FileInputStream("C:/Users/MSB/Desktop/Haha/" + args[0]);
          DataInputStream in1 = new DataInputStream(fstream1);
          BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
           
          String strLine = null; //PointIndex: catching the String value from the stringTokenizer
          String strLine1 = null;//text file: catching the String value from the stringTokenizer
          String keyword = null;     // We put keyword from ArraylistForKeyWords  
         
          int keyWordIndex = 0;     // how many key words do we have ?
          int documentCounter = 1; // how many documents do we have ?
          int hashindex = 0;       // this is index for arraylist. 
          int detectDoc = 0;
          //HowManyTimeKeyWordAppearInEachDoc <which doccument , home many time appear>
          int HowManyTimeKeywordAppearInThisDoc = 0; //  how many times appear
          int savecounter = 0;        // which document 
          int howManyDocAppear = 0 ; // this index is showing that many time keywords are appearing on the while text file. 
          
        
          Hashtable<Integer , String> htDocCounter = new Hashtable<Integer , String>();
          // this is the hashtable to store the document numbers which contain keyword
          Hashtable<Integer, Integer> DocsThatKeywordAppear = new Hashtable<Integer, Integer>();
          // this is the hashtable to store how many time keyword is appear on each doc 
          Hashtable<Integer, Integer> HowManyTimeKeyWordAppearInEachDoc = new Hashtable<Integer, Integer>();
          // this is arraylast to store the keyword from the text file.
          ArrayList<String> ArraylistForKeyWords= new ArrayList<String>();
          
          // First while loop 
          // it is reading the file and storing unique keywords 
          while((strLine1 = br1.readLine()) != null)
          {
                  StringTokenizer stringTokenizer1 = new StringTokenizer(strLine1);
                  while(stringTokenizer1.hasMoreTokens())
                  {
                          String tokenString =  stringTokenizer1.nextToken().toLowerCase();

                          if(!ArraylistForKeyWords.contains(tokenString))
                          {// if it is unique keyword then store in the arraylist
                        	  ArraylistForKeyWords.add(tokenString);            
                          }
                  }

          }
          // since we need to display the results by order, I sort the arraylist.
          Collections.sort(ArraylistForKeyWords);         
          while(hashindex < ArraylistForKeyWords.size()){ // going until we read all of keywords in the arraylist
                  keyword = ArraylistForKeyWords.get(hashindex); // get the keyword from the arrayList
                  // get paragraph for comparing
                  FileInputStream fstream = new FileInputStream("C:/Users/MSB/Desktop/Haha/" + args[0]); 
                  DataInputStream in = new DataInputStream(fstream);
                  BufferedReader br = new BufferedReader(new InputStreamReader(in));
                  while ((strLine = br.readLine()) != null)   { // starting to read line by line
                         StringTokenizer stringTokenizer = new StringTokenizer(strLine);
                         int EndOftheParagraph = 0; // We can find the end of the paragraph when value is 0
                                       
                         Scanner sc = new Scanner (strLine);
                         while(sc.hasNext())
                         {
                            sc.next();
                            EndOftheParagraph++;
                         }                                     
                         if(EndOftheParagraph == 0)
                         {// It is the end of paragraph, so we are reading new file
                             savecounter++; //increase counter for next document   
                             HowManyTimeKeywordAppearInThisDoc = 0;// since we are moving to new doc
                             									   // , we need to count from 0 again                             									
                         }
                         if(stringTokenizer.countTokens() == 0)
                         { // if token is done it's end of doc
                             documentCounter++;   
                             // if it is moving to new document, we need to initialize 
                             // HowManyTimeKeywordAppearInThisDoc again for counting keyword from
                             // another doc                                                                                                                                   
                         }
                         while(stringTokenizer.hasMoreElements()){ // This is the part for reading text file to compare keyword
                               String lowercaseWordFromParagraph = stringTokenizer.nextToken().toLowerCase(); // get all lower case word
                               		if(lowercaseWordFromParagraph.equals(keyword)){ // if we found the keyword from the paragraph
                                                     
                               			HowManyTimeKeywordAppearInThisDoc++; // increase number of keyword in this document
                                    // this hashtabl is recording how many time keyword is appearing in each document
                               			HowManyTimeKeyWordAppearInEachDoc.put(savecounter, HowManyTimeKeywordAppearInThisDoc);                                                                                                              
                                    	if(!htDocCounter.containsKey(documentCounter))                                                     
                                    	{// If new document contains keyword             
                                    		htDocCounter.put(documentCounter, lowercaseWordFromParagraph); // we put keyword in the hashtable
                                    		DocsThatKeywordAppear.put(detectDoc, documentCounter); // we also put document number in the hashtable
                                    		detectDoc++; // for next keyword, we increare key in hashtable
                                    		howManyDocAppear++;                                                                                      
                                    	}                    
                                        keyWordIndex++;                                                                                                              
                               		}
                                                                                       
                        }       
                }
                  ///////////// Printing and initializing part//////////////                 
                  System.out.println(keyword); // print keyword
                  System.out.print(howManyDocAppear + "," + keyWordIndex ); // how many docs contain keyword
                                                                            // how many time appear in each doc                    
                  // To make display form, we copy two hashtables values to arraylist.
                  ArrayList<Integer> list1 = new ArrayList<Integer>(DocsThatKeywordAppear.values());
                  ArrayList<Integer> list2 = new ArrayList<Integer>(HowManyTimeKeyWordAppearInEachDoc.values());
                  ReverceList(list1);
                  ReverceList(list2);
                  //Printing part "(which document, hom many times)"
                  for(int printerIndex = 0 ; printerIndex <list1.size() ; printerIndex++)
                  {
                	  System.out.print(",("+ list1.get(printerIndex) + ",");
                	  System.out.print( list2.get(printerIndex) + ")");
                  }
                  NewLine();
                  NewLine();
                  // one looping is done , so we need to re-set all variable
                  // and hashtable for another keyword
                  keyWordIndex = 0;
                  howManyDocAppear = 0;
                  savecounter = 0;
                  detectDoc =0;
                  documentCounter = 1;
                  hashindex++;
                  free(htDocCounter);
                  free(DocsThatKeywordAppear);
                  free(HowManyTimeKeyWordAppearInEachDoc);                
          }        
        }
		/*
		 * Helper Function 
		 * */
		//// For Printing New Line
		public static void NewLine()
		{System.out.println();}
		// For reversing the ArrayList 
		public static void ReverceList(ArrayList<Integer> al)
		{Collections.reverse(al);}
		// Clearing contents inside of Hashtable
		public static void free(Hashtable ht)
		{ht.clear();}
}		